﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using OpenSunTracker.RestClient;
using OpenSunTracker.RestClient.Extensions;

namespace OSTWin
{
    /// <summary>
    /// Interaction logic for ucConfiguration.xaml
    /// </summary>
    public partial class ucConfiguration : UserControl
    {
        public ucConfiguration()
        {
            InitializeComponent();
        }

        public OpenSunTrackerClient Client { get { return DataContext as OpenSunTrackerClient; } }

        private void btnChangeConfig_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var btn = sender as Button;

                if (btn == null)
                    throw new Exception("sender is no button");

                var assTxt = this.FindName(btn.Tag.ToString()) as TextBox;

                if (assTxt == null) 
                    throw new Exception("no assiciated text-box found");

                var changeUrl = string.Format(assTxt.Tag.ToString(), assTxt.Text);
                changeUrl = Client.Url + "/configuration/" + changeUrl;

                var response = changeUrl.ExecuteReadRequest();

                Client.AddMessage("response on '"+ changeUrl + "' was '" +response.ReadAllText() + "'");
            }
            catch (Exception ex)
            {
                Client.AddMessage("error on config-change: " + ex.Message);
            }
        }

        private void cmb_SelectionChanged(object sender, RoutedEventArgs e)
        {
            try
            {
                var btn = sender as Button;

                if (btn == null)
                    throw new Exception("no assiciated Combo-Button found");

                var assCmb = this.FindName(btn.Tag.ToString()) as ComboBox;

                if (assCmb == null)
                    throw new Exception("no assiciated combo found");

                var changeUrl = string.Format(assCmb.Tag.ToString(), assCmb.SelectedIndex);
                changeUrl = Client.Url + "/configuration/" + changeUrl;

                var response = changeUrl.ExecuteReadRequest();

                Client.AddMessage("response on '" + changeUrl + "' was '" + response.ReadAllText() + "'");
            }
            catch (Exception ex)
            {
                Client.AddMessage("error on config-change-combo: " + ex.Message);
            }
        }

        private void chk_Changed(object sender, RoutedEventArgs e)
        {
            try
            {
                var btn = sender as Button;

                if (btn == null)
                    throw new Exception("no assiciated Button-CheckBox found");

                var assChk = this.FindName(btn.Tag.ToString()) as CheckBox;

                if (assChk == null)
                    throw new Exception("no assiciated combo found");

                var newValue = string.Empty;

                if (assChk.IsChecked.Value)
                    newValue = "True";
                else
                    newValue = "False";

                var changeUrl = string.Format(assChk.Tag.ToString(), newValue);
                changeUrl = Client.Url + "/configuration/" + changeUrl;

                var response = changeUrl.ExecuteReadRequest();

                Client.AddMessage("response on '" + changeUrl + "' was '" + response.ReadAllText() + "'");
            }
            catch (Exception ex)
            {
                Client.AddMessage("error on checkbox-check: " + ex.Message);
            }
        }

    }
}
